Ext.define("MyFirstApp.controller.Server", {
    extend: "Ext.app.Controller",
    config: {
        refs: {
            // We're going to lookup our views by xtype.
            servercontainer: 'servercontainer',
            list: 'servercontainer server list',
			serverstatus: 'statushistory',
            searchfield: 'searchfield'
        },
        control: {
            list: {
                itemtap: 'onItemtap'
            },
            searchfield: {
                keyup: 'onSearchKeyUp',
                clearicontap: 'onSearchClearIconTap'
            }
        }
    },
    onItemtap: function (list, index, target, record, e, eOpts){
        if(!this.getServerstatus()) {
            console.log("Item " + record.get("id") + " tapped");
            var serverId = record.get("id");
            var statusStore = Ext.getStore("StatusHistories");
            var statusStoreProxy = statusStore.setProxy({
                type:'jsonp',
                url:'http://scesvrstatus.sinaapp.com/history/' +serverId+ '.json',
                reader:{
                    type:'json',
                    rootProperty: 'history'
                }
            });
            statusStoreProxy.load();
            //console.log(statusStore);
            var statusHistory = Ext.create('MyFirstApp.view.StatusHistory', {
                title: record.get("name")
            });
            this.getServercontainer().push(statusHistory);
        }
    },
    onSearchKeyUp: function(field){
        var value = field.getValue();
        var store = Ext.getStore("serversJsonStore");
        store.clearFilter(!!value); // what is !!
        if (value) {
            console.log("搜索栏有值..");
            var searches = value.split(','),
                regexps = [],
                i, regex;
            for (i = 0; i < searches.length; i++) {
                //if it is nothing, continue
                if (!searches[i]) continue;

                regex = searches[i].trim();
                regex = regex.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

                //if found, create a new regular expression which is case insenstive
                regexps.push(new RegExp(regex.trim(), 'i'));
            }
            store.filter(function(record) {
                var matched = [];

                //loop through each of the regular expressions
                for (i = 0; i < regexps.length; i++) {
                    var search = regexps[i],
                        didMatch1 = search.test(record.get('name')),
                        didMatch2 = search.test(record.get('server'));
                    //if it matched the first or last name, push it into the matches array
                    matched.push(didMatch1,didMatch2);
                    //matched.push(didMatch2);
                }

                return (regexps.length && matched.indexOf(true) !== -1);
            });
        }
    },
    onSearchClearIconTap: function() {
        console.log("search cleaned...");
        Ext.getStore("serversJsonStore").clearFilter();
    }
});